///PHILOTES Source Code.  (C)2012 PhiloLabs

#include "OgreCgPlugin.h"
#include "OgreRoot.h"
#include "OgreHighLevelGpuProgramManager.h"
#include "OgreCgFxScriptLoader.h"

namespace Ogre 
{
	const String sPluginName = "Cg Program Manager";
	//---------------------------------------------------------------------
	CgPlugin::CgPlugin()
		:mCgProgramFactory(0)
	{

	}
	//---------------------------------------------------------------------
	const String& CgPlugin::getName() const
	{
		return sPluginName;
	}
	//---------------------------------------------------------------------
	void CgPlugin::install()
	{
	}
	//---------------------------------------------------------------------
	void CgPlugin::initialise()
	{
        // check for gles2 by the glsles factory (this plugin is not supported on embedded systems for now)
        if (HighLevelGpuProgramManager::getSingleton().isLanguageSupported("glsles") == false)
        {
            // Create new factory
            mCgProgramFactory = PHILO_NEW CgProgramFactory();

            // Register
            HighLevelGpuProgramManager::getSingleton().addFactory(mCgProgramFactory);

            PHILO_NEW CgFxScriptLoader();
        }
	}
	//---------------------------------------------------------------------
	void CgPlugin::shutdown()
	{
		// nothing to do
	}
	//---------------------------------------------------------------------
	void CgPlugin::uninstall()
	{
        if (mCgProgramFactory)
        {
			PHILO_DELETE CgFxScriptLoader::getSingletonPtr(); 

            // Remove from manager safely
            if (HighLevelGpuProgramManager::getSingletonPtr())
                HighLevelGpuProgramManager::getSingleton().removeFactory(mCgProgramFactory);
		    PHILO_DELETE mCgProgramFactory;
		    mCgProgramFactory = 0;
        }
	}
}
